package mod.luckymod.util;

import net.minecraft.core.BlockPos;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

import static mod.luckymod.Luckymod.scheduler;

public class ScheduleConditionalTask {
    public void scheduleConditionalAction(boolean condition, @NotNull Level level, @NotNull BlockPos pos,
                       int delaySeconds, @Nullable BiConsumer<Level, BlockPos> startAction,
                                          @NotNull BiConsumer<Level, BlockPos> endAction) {
        if (condition && !level.isClientSide) {
            // 执行开始操作
            if (startAction != null) {
                startAction.accept(level, pos);
            }
            // 安排延迟执行任务
            scheduler.schedule(() -> {
                // 确保在服务器主线程执行
                if (level instanceof ServerLevel) {
                    MinecraftServer server = level.getServer();
                    server.execute(() -> {
                        // 执行结束操作
                        endAction.accept(level, pos);
                    });
                }
            }, delaySeconds, TimeUnit.SECONDS);
        }
    }
}
